package com.ruoyi.common.im.service;

import io.rong.RongCloud;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupUserQueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * IM群组服务
 *
 * @author jzy
 * @date 2021/11/13
 */
@Service
public class GroupService {


    @Autowired
    private RongCloud rongCloud;

    /**
     * 创建群组
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#create
     *
     * @param groupId   群组id
     * @param groupName 群组名称
     * @param memberIds 群组成员
     * @return
     */
    public Result createGroup(String groupId, String groupName, List<String> memberIds) {
        GroupModel group = new GroupModel().setId(groupId).setName(groupName);
        if (memberIds != null && memberIds.size() > 0) {
            group.setMembers(memberListToArray(memberIds));
        }
        Result groupCreateResult = null;
        try {
            groupCreateResult = rongCloud.group.create(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groupCreateResult;
    }

    /**
     * 更新群组
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#refresh
     *
     * @param groupId   群组id
     * @param groupName 群组名称
     * @return
     */
    public Result updateGroup(String groupId, String groupName) {
        GroupModel group = new GroupModel().setId(groupId).setName(groupName);
        Result result = null;
        try {
            result = rongCloud.group.update(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 加入群聊
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#join
     *
     * @param groupId   群组id
     * @param groupName 群组名称
     * @param memberIds 群组成员
     * @return
     */
    public Result join(String groupId, String groupName, List<String> memberIds) {
        GroupModel group = new GroupModel().setId(groupId).setName(groupName);
        if (memberIds != null && memberIds.size() > 0) {
            group.setMembers(memberListToArray(memberIds));
        }
        Result groupCreateResult = null;
        try {
            groupCreateResult = rongCloud.group.join(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groupCreateResult;
    }

    /**
     * 获取群组成员
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#getMembers
     *
     * @param groupId 群id
     * @return
     */
    public GroupUserQueryResult memberList(String groupId) {
        GroupModel groupModel = new GroupModel().setId(groupId);
        GroupUserQueryResult getMemberesult = null;
        try {
            getMemberesult = rongCloud.group.get(groupModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getMemberesult;
    }


    /**
     * 退出群聊
     *
     * @param groupId   群组id
     * @param memberIds 成员ids(要退出的成员)
     * @return
     */
    public Result quit(String groupId, List<String> memberIds) {
        GroupModel groupModel = new GroupModel().setId(groupId);
        if (memberIds != null && memberIds.size() > 0) {
            groupModel.setMembers(memberListToArray(memberIds));
        }
        Result groupQuitResult = null;
        try {
            groupQuitResult = rongCloud.group.quit(groupModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groupQuitResult;
    }


    /**
     * 解散群组
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#dismiss
     *
     * @param groupId 群组id
     * @param userId  解散人id
     * @return
     */
    public Result dismiss(String groupId, String userId) {
        GroupModel groupModel = new GroupModel().setId(groupId);
        GroupMember[] members = new GroupMember[]{new GroupMember().setId(userId)};
        groupModel.setMembers(members);
        Result groupQuitResult = null;
        try {
            groupQuitResult = rongCloud.group.dismiss(groupModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groupQuitResult;
    }


    public GroupMember[] memberListToArray(List<String> memberIds) {
        if (memberIds != null && memberIds.size() > 0) {
            GroupMember[] members = new GroupMember[memberIds.size()];
            for (int i = 0; i < memberIds.size(); i++) {
                members[i] = new GroupMember().setId(memberIds.get(i));
            }
            return members;
        }
        return null;
    }
}
